﻿var comboBox;
var firstLoading = true;

$(function () {
    $("#sex").ligerComboBox({
        cancelable: false,
        width: 120,
        valueFieldID: 'hdnSex'
    });

    $("#txtBirthday").ligerDateEditor({
        showTime: false,
        format: "yyyy-MM-dd",
        width: 120,
        cancelable: false
    });

    $("#powerRole").ligerComboBox({
        url: "/Sysbase/Role/GetRoleList?ram" + Math.random(),
        cancelable: false,
        width: 120,
        valueField: "RoleCode",
        textField: "RoleName",
        valueFieldID: 'hdnPowerRole',
        value: $("#powerRole").val()
    });

    $("#jobCode").ligerComboBox({
        url: "/Sysbase/User/GetJoblist?ram" + Math.random(),
        cancelable: false,
        width: 120,
        valueField: "JobCode",
        textField: "JobTitle",
        valueFieldID: 'hdnJobCode',
        value: $("#jobCode").val()
    });

    $("#positionCode").ligerComboBox({
        url: "/Sysbase/User/GetPositionlist?ram" + Math.random(),
        cancelable: false,
        width: 120,
        valueField: "PositionCode",
        textField: "PositionTitle",
        valueFieldID: 'hdnPositionCode',
        value: $("#positionCode").val()
    });

    bindDepartment();
});

function bindDepartment() {
    comboBox = $("#departmentCode").ligerComboBox({
        width: 370,
        slide: false,
        selectBoxWidth: 370,
        selectBoxHeight: 220,
        valueField: 'DepartmentCode',
        textField: 'DepartmentName',
        valueFieldID: 'hdnDepartmentCode',
        tree: {
            url: '/Sysbase/Department/GetAllDepartment?ram' + Math.random(),
            parms: { isAll: 0 },
            ajaxType: 'get',
            idFieldName: "DepartmentCode",
            parentIDFieldName: "ParentId",
            textFieldName: "DepartmentName",
            onSuccess: function (data) {
                if ($("#hdnDepartmentCodeValue").val() != "") {
                    comboBox.setValue($("#hdnDepartmentCodeValue").val().replace(/&/g, ';'));
                }
            }
        }
    });
}

function save() {
    var id = $("#hdnId").val();
    var userId = $("#txtUserId").val();
    var userName = $("#txtUserName").val();
    var password = $("#txtPassword").val();
    var departmentCode = $("#hdnDepartmentCode").val().replace(/;/g, "&");
    var departmentName = $("#departmentCode").ligerComboBox().getText().replace(/;/g, "&");
    var jobCode = $("#hdnJobCode").val();
    var jobTitle = $("#jobCode").ligerComboBox().getText();
    var positionCode = $("#hdnPositionCode").val();
    var positionTitle = $("#positionCode").ligerComboBox().getText();
    var sex = $("#hdnSex").val();
    var ICCode = $("#txtICCode").val();
    var barCode = $("#txtBarCode").val();
    var telephone = $("#txtTelephone").val();
    var birthday = $("#txtBirthday").val();
    var major = $("#txtMajor").val();
    var remarks = $("#txtRemarks").val();
    var powerRole = $("#hdnPowerRole").val();
    var roleName = $("#powerRole").ligerComboBox().getText();
    var status = $('input[name="status"]:checked').val();

    if (IsEmpty(userName)) {
        $.ligerDialog.error("姓名不能为空");
        $("#txtUserName").focus();
        return;
    }
    if (IsEmpty(userId)) {
        $.ligerDialog.error("工号不能为空");
        $("#txtUserId").focus();
        return;
    }
    if (IsEmpty(password)) {
        $.ligerDialog.error("密码不能为空");
        $("#txtPassword").focus();
        return;
    }
    if (IsEmpty(powerRole)) {
        $.ligerDialog.error("角色不能为空");
        $("#powerRole").focus();
        return;
    }
    if (IsEmpty(jobCode)) {
        $.ligerDialog.error("职务不能为空");
        $("#jobCode").focus();
        return;
    }
    if (IsEmpty(positionCode)) {
        $.ligerDialog.error("职称不能为空");
        $("#positionCode").focus();
        return;
    }
    if (IsEmpty(departmentCode)) {
        $.ligerDialog.error("所属部门不能为空");
        $("departmentCode").focus();
        return;
    }

    $.ajax({
        url: "/Sysbase/User/Save",
        type: "post",
        data: {
            Id: id,
            UserId: userId,
            UserName: userName,
            DepartmentCode: departmentCode,
            DepartmentName: departmentName,
            JobCode: jobCode,
            JobTitle: jobTitle,
            PositionCode: positionCode,
            PositionTitle: positionTitle,
            Password: password,
            Sex: sex,
            ICCode: ICCode,
            BarCode: barCode,
            Telephone: telephone,
            Birthday: birthday,
            Major: major,
            Remarks: remarks,
            PowerRole: powerRole,
            RoleName: roleName,
            Status: status
        },
        success: function (r) {
            var r = $.parseJSON(r);
            if (r.Result == 1) {
                parent.$("#btnSearch").click();
                closeDialog();
            }
            else if (r.Result == 0) {
                $.ligerDialog.error(r.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}

function closeDialog() {
    frameElement.dialog.close();//关闭dialog
}